<?php 
/**
 * Write Log
 */
defined ( 'IN_SYS' ) or die ( 'Restricted Access' );
class SysLogs {
	static private $_is_log = true;
	static private $_log_sql = array();
	static private $_sql_tmp = array('sql_begin'=>'','sql_end'=>'', 'time_begin'=>'', 'time_end'=>'');
	// y:year, m:month, d:day, w:week, h:hour
	static private $file_extention = array('y'=>'Y','m'=>'Y-m','d'=>'Y-m-d','w'=>'Y_W','h'=>'Y-m-d_H');
	static private $date_current;
	
	// Log error
	static private $_log_error = array();
	
	function __construct() {
		
	}
	
	static function log($fileName, $buffer) {
		$fileName = $fileName . '.log';
		
		if (file_exists(SYS_LOG . $fileName)) {
			$content = "\n\n";
		} else {
			$content = '--['.date(self::$date_current).']'."\n\n";
		}
		
		$handle = @fopen(SYS_LOG . $fileName, 'a');
		if ($handle) {
			$content.= $buffer . "\n" . '--['.$_SERVER['REQUEST_URI'] . "]";
			fwrite($handle, $content);
			fclose($handle);
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * push sql
	 */
	static function pushLogSql($sql='') {
		self::$_sql_tmp['sql_begin'] = $sql;
		self::$_sql_tmp['time_begin'] = self::getMicrotime();
	}
	
	/**
	 * end log sql
	 */
	static function endLogSql($sql='') {
		self::$_sql_tmp['sql_end'] = $sql;
		self::$_sql_tmp['time_end'] = self::getMicrotime();
		self::$_log_sql[] = self::$_sql_tmp;
	}
	
	/**
	 * switch write log SQL
	 */
	static function writeLogSql() {
		if (LOG_SQL !== true) return;
		
		foreach (self::$_log_sql as $rs) {
			$sql_tmp = strtoupper($rs['sql_end']);
			if (LOG_SQL_SLOW > 0 && LOG_SQL_SLOW <= ($rs['time_end']-$rs['time_begin'])) {
				self::logSql($rs, 'SQL_SLOW');
			}
			
			if (strpos(LOG_SQL_TYPE,'DEL')!==false && preg_match('/^(.*)DELETE(.*)FROM(.*)+$/sm', $sql_tmp)) {
				self::logSql($rs, 'SQL_DEL');
				self::logSql($rs, 'SQL');
				continue;
			}
			
			if (strpos(LOG_SQL_TYPE,'ADD')!==false && preg_match('/^(.*)INSERT(.*)INTO(.*)+$/sm', $sql_tmp)) {
				self::logSql($rs, 'SQL_ADD');
				self::logSql($rs, 'SQL');
				continue;
			}
			
			if (strpos(LOG_SQL_TYPE,'EDIT')!==false && (preg_match('/^(.*)UPDATE(.*)SET(.*)+$/sm', $sql_tmp) || preg_match( '/^(.*)REPLACE(.*)INTO(.*)+$/sm', $sql_tmp))) {
				self::logSql($rs, 'SQL_EDIT');
				self::logSql($rs, 'SQL');
				continue;
			}
			
			if (strpos(LOG_SQL_TYPE,'VIEW')!==false && preg_match('/^(.*)SELECT(.*)FROM(.*)+$/sm', $sql_tmp)) {
				self::logSql($rs, 'SQL_VIEW');
				self::logSql($rs, 'SQL');
				continue;
			}
			
			if (strpos(LOG_SQL_TYPE,'OTHER')!==false) {
				if (preg_match('/^(.*)DELETE(.*)FROM(.*)+$/sm', $sql_tmp)) continue;
				if (preg_match('/^(.*)INSERT(.*)INTO(.*)+$/sm', $sql_tmp)) continue;
				if (preg_match('/^(.*)UPDATE(.*)SET(.*)+$/sm', $sql_tmp) || preg_match( '/^(.*)REPLACE(.*)INTO(.*)+$/sm', $sql_tmp)) continue;
				if (preg_match('/^(.*)SELECT(.*)FROM(.*)+$/sm', $sql_tmp)) continue;
				
				self::logSql($rs, 'SQL_OTHER');
				self::logSql($rs, 'SQL');
				continue;
			}
		}
	}
	
	/**
	 * return log sql array
	 */
	static public function getLogSql() {
		return self::$_log_sql;
	}
	
	
	/**
	 * write log SQL
	 */
	static private function logSql($rs=array(), $name='SQL') {
		$time = $rs['time_end'] - $rs['time_begin'];
			
		$content = $rs['sql_end'] . ";\n";
		$content.= "--[" . date('Y/m/d H:i:s', $rs['time_begin']) . ']' . "\n";
		$content.= '--Run time (s): ' . $time;
		
		if (isset(self::$file_extention[LOG_SQL_TIME])) {
			self::$date_current = self::$file_extention[LOG_SQL_TIME];
		} else {
			self::$date_current = self::$file_extention['d'];
		}
		
		$fileName = 'sql'.DS.$name.'_'.date(self::$date_current, $rs['time_end']);
		self::log($fileName, $content);
	}
	
	/**
	 * write log error
	 */
	static function logError($error='') {
		self::$_log_error[] = $error;
		
		if (LOG_ERROR === TRUE) {
			if (isset(self::$file_extention[LOG_ERROR_TIME])) {
				self::$date_current = self::$file_extention[LOG_ERROR_TIME];
			} else {
				self::$date_current = self::$file_extention['d'];
			}
			
			$fileName = 'error'.DS.'ERROR_'.date(self::$date_current, self::getMicrotime());
			self::log($fileName, $error);
		}
	}
	
	/**
	 * return error on system
	 */
	static function getLogError() {
		return self::$_log_error;
	}
	
	/**
	 * Get Microtime
	 *
	 * @return float
	 */
	static public function getMicrotime() {
		list ( $usec, $sec ) = explode ( ' ', microtime () );
		return (( float ) $usec + ( float ) $sec);
	}
}